package awt;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class MyWindow extends JFrame {
	public static final String[] names = { "王鑫鑫", "陈国运", "汪翰林", " 阚顺付 ", "姚宇晨", "黄成芳", "胡亚飞"

			, "孙辰宇", "蔡启明", "陈怀阳", "杨澜宇", "梅周", "章冬生", "许家斌", "齐丹东", "马超", "王继伟", "何志星", "陈哲", "汪浩斌", "汪浩", "张洋",
			"汤美龙" };

	private Object LOCK = new Object();

	public MyWindow(String t) {
		super(t);
		// 关闭
		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		super.setLayout(null);
		// 加入新元素·
		final JTextField j1 = new JTextField();
		j1.setBounds(20, 30, 80, 40);
		j1.setColumns(12);
		super.add(j1);
		final Random rand = new Random();
		// final JButton jbutton = new JButton("start");
		// jbutton.setBounds(160, 30, 100, 40);
		class TimeThread extends Thread {
			boolean runing = true;

			@Override
			public void run() {
				while (true) {
					if (runing) {
						j1.setText(names[rand.nextInt(names.length)]);
						try {
							sleep(5);
						} catch (InterruptedException e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();
						}
					} else {
						synchronized (LOCK) {
							try {
								System.out.println("线程等待");
								LOCK.wait();
							} catch (InterruptedException e) {
								// TODO 自动生成的 catch 块
								e.printStackTrace();
							}
						}
					}

				}
			}
		}
		final TimeThread th = new TimeThread();

		// jbutton.addActionListener(new ActionListener() {
		// @Override
		// public void actionPerformed(ActionEvent e) {
		// // System.out.println("按钮被点击了");
		// // JOptionPane.showMessageDialog(MyWindow.this, "按钮被点击了,您输入的是"
		// // + j1.getText());
		// th.runing = true;
		// //
		// }
		// });
		// super.add(jbutton);

		final JButton jbutton2 = new JButton("stop");
		jbutton2.setBounds(320, 30, 100, 40);
		jbutton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (th.runing) {
					th.runing = false;
					synchronized (LOCK) {
						System.out.println(Thread.currentThread().getName() + "唤醒其他线程111111111");
						LOCK.notifyAll();
					}
				} else {
					th.runing = true;
					synchronized (LOCK) {
						System.out.println(Thread.currentThread().getName() + "唤醒其他线程222222222");
						LOCK.notifyAll();
					}
				}

			}

		});
		super.add(jbutton2);

		th.start();
	}

	public static void main(String[] args) {
		MyWindow m = new MyWindow("javA窗体");

		m.setSize(700, 600);
		m.setVisible(true);
	}
}
